package com.chinacoal.microservice.impl.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.chinacoal.microservice.api.AgentClient;
import com.chinacoal.microservice.exception.MessageException;
import com.chinacoal.microservice.impl.service.AgentService;
import com.chinacoal.microservice.impl.service.WorkFlowService;
import com.chinacoal.microservice.impl.util.ProcessFun;
import com.chinacoal.microservice.login.LoginUser;
import com.chinacoal.microservice.model.IamUser;
import com.chinacoal.microservice.model.wofkflow.QueryAgentParam;
import com.chinacoal.microservice.model.wofkflow.entity.AgentEntity;
import com.chinacoal.microservice.util.result.CodeMsg;
import com.chinacoal.microservice.util.result.Result;
import com.eos.workflow.api.IBPSServiceClient;

/**  
 * @Package: com.chinacoal.microservice.impl.controller.AgentController
 * @ClassName: AgentController
 * @ClassDesc: 审批代理Controller
 * @Createby: sunh
 * @CreatedDate: 2019-11-29 08:55:58
 */  
@RestController
public class AgentController implements AgentClient{

	@Resource
	private AgentService agentService;
	@Resource
	private WorkFlowService workFlowService;
	@Resource
	private LoginUser loginUser;
	
	@Override
	public Result<Page<AgentEntity>> findList(int page, int pageSize, QueryAgentParam param) {

		try {
			IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new MessageException("用户未登录");

			param.setUserId(iamUser.getUserId());
			
			Page<AgentEntity> agentPage = agentService.findList(page, pageSize, param);
			
			return Result.success(agentPage);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(ProcessFun.getExceptionMessage(e)));
		}
	}
	
	@Override
	public Result<AgentEntity> findOne(Long agentId){
		try {
			AgentEntity agentEntity = agentService.findOne(agentId);
			return Result.success(agentEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(ProcessFun.getExceptionMessage(e)));
		}
	}
	
	@Override
	public Result<Object> insert(AgentEntity agentEntity) {
		try {
			IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new MessageException("用户未登录");
			
			// 获取工作流服务
			IBPSServiceClient client = workFlowService.getIBPSServiceClient(iamUser.getUserId(), iamUser.getName());

			agentEntity.setUserId(iamUser.getUserId());
			agentService.insertAgent(client, agentEntity);
			
			return Result.success("新增代理成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(ProcessFun.getExceptionMessage(e)));
		}
	}

	@Override
	public Result<Object> update(@Valid AgentEntity agentEntity) {
		try {
			IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new MessageException("用户未登录");
			
			// 获取工作流服务
			IBPSServiceClient client = workFlowService.getIBPSServiceClient(iamUser.getUserId(), iamUser.getName());

			agentEntity.setUserId(iamUser.getUserId());
			agentService.updateAgent(client, agentEntity);
			
			return Result.success("修改代理成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(ProcessFun.getExceptionMessage(e)));
		}
	}

	@Override
	public Result<Object> delete(Long agentId) {
		try {
			IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new MessageException("用户未登录");
			
			// 获取工作流服务
			IBPSServiceClient client = workFlowService.getIBPSServiceClient(iamUser.getUserId(), iamUser.getName());

			agentService.deleteAgent(client, agentId);
			
			return Result.success("删除代理成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(ProcessFun.getExceptionMessage(e)));
		}
	}

	@Override
	public Result<Object> deleteItem(Long agentItemId) {
		try {
			
			agentService.deleteAgentItem(agentItemId);
			
			return Result.success("删除行数据成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(ProcessFun.getExceptionMessage(e)));
		}
	}
	
}
